var validator_outer = require('./validator/index')

/**
 * 验证是否为有效手机号码
 * @param {手机号字符串} phone 
 */
function isPhone(phone) {
    var reg = /^1[3456789]\d{9}$/;
    if (reg.test(phone)) {
        return true
    } else {
        return false
    }
}

/**
 * 验证是否为有效电子邮箱格式
 * @param {电子邮箱} email 
 */
function isEmail(email) {
    var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
    if (reg.test(email)) {
        return true
    } else {
        return false
    }
}

/**
 * 检测是否为身份证号
 * @param {身份证号码} cardId 
 */
function isIDCard(cardId) {

    if (cardId === '') {
        return false;
    }

    return _isCardNo(cardId) && _isCardProvince(cardId) && _isCardBirthday(cardId) && _isParity(cardId)
}

/**
 * 省份集合
 */
var _vcity = {
    11: "北京",
    12: "天津",
    13: "河北",
    14: "山西",
    15: "内蒙古",
    21: "辽宁",
    22: "吉林",
    23: "黑龙江",
    31: "上海",
    32: "江苏",
    33: "浙江",
    34: "安徽",
    35: "福建",
    36: "江西",
    37: "山东",
    41: "河南",
    42: "湖北",
    43: "湖南",
    44: "广东",
    45: "广西",
    46: "海南",
    50: "重庆",
    51: "四川",
    52: "贵州",
    53: "云南",
    54: "西藏",
    61: "陕西",
    62: "甘肃",
    63: "青海",
    64: "宁夏",
    65: "新疆",
    71: "台湾",
    81: "香港",
    82: "澳门",
    91: "国外"
};

/**
 * 检查号码是否符合规范，包括长度，类型
 * @param {身份证号} cardId 
 */
function _isCardNo(cardId) {
    //身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X  
    var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/;
    if (reg.test(cardId) === false) {
        return false;
    }

    return true;
}

/**
 * 取身份证前两位,校验省份  
 * @param {身份证号} cardId 
 */
function _isCardProvince(cardId) {
    var province = cardId.substr(0, 2);
    if (_vcity[province] == undefined) {
        return false;
    }
    return true;
};

/**
 * 校验生日
 * @param {身份证号} cardId 
 */
function _isCardBirthday(cardId) {
    var len = cardId.length;
    //身份证15位时，次序为省（3位）市（3位）年（2位）月（2位）日（2位）校验位（3位），皆为数字  
    if (len == '15') {
        var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/;
        var arr_data = cardId.match(re_fifteen);
        var year = arr_data[2];
        var month = arr_data[3];
        var day = arr_data[4];
        var birthday = new Date('19' + year + '/' + month + '/' + day);
        return _verifyBirthday('19' + year, month, day, birthday);
    }
    //身份证18位时，次序为省（3位）市（3位）年（4位）月（2位）日（2位）校验位（4位），校验位末尾可能为X  
    if (len == '18') {
        var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/;
        var arr_data = cardId.match(re_eighteen);
        var year = arr_data[2];
        var month = arr_data[3];
        var day = arr_data[4];
        var birthday = new Date(year + '/' + month + '/' + day);
        return _verifyBirthday(year, month, day, birthday);
    }
    return false;
}

function _verifyBirthday(year, month, day, birthday) {
    var now = new Date();
    var now_year = now.getFullYear();
    //年月日是否合理  
    if (birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day) {
        //判断年份的范围（3岁到100岁之间)  
        var time = now_year - year;
        if (time >= 3 && time <= 100) {
            return true;
        }
        return false;
    }
    return false;
}

/**
 * 校验位的检测
 * @param {身份证号} cardId 
 */
function _isParity(cardId) {

    //15位转18位  
    cardId = _changeFivteenToEighteen(cardId);
    var len = cardId.length;
    if (len == '18') {
        var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
        var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
        var cardTemp = 0,
            i, valnum;
        for (i = 0; i < 17; i++) {
            cardTemp += cardId.substr(i, 1) * arrInt[i];
        }
        valnum = arrCh[cardTemp % 11];
        if (valnum == cardId.substr(17, 1)) {
            return true;
        }
        return false;
    }
    return false;
}

/**
 * 15位转18位身份证号
 * @param {身份证号} card 
 */
function _changeFivteenToEighteen(card) {
    if (card.length == '15') {
        var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
        var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
        var cardTemp = 0,
            i;
        card = card.substr(0, 6) + '19' + card.substr(6, card.length - 6);
        for (i = 0; i < 17; i++) {
            cardTemp += card.substr(i, 1) * arrInt[i];
        }
        card += arrCh[cardTemp % 11];
        return card;
    }
    return card;
};


/**
 * 检测对象是否为方法类型
 * @param {需要检测的对象} 需要检测的对象 
 */
function isFunction(target) {
    return 'function' === typeof target;
}

/**
 * 是否为空字符串（包含情况：null、""、undefined）
 * @param {需要检测的对象} target 
 */
function isNullOrEmpty(target) {
    return target == null || target == "" || target == undefined
}

exports.version = validator_outer.version
exports.toDate = validator_outer.toDate
exports.toFloat = validator_outer.toFloat
exports.toInt = validator_outer.toInt
exports.toBoolean = validator_outer.toBoolean
exports.equals = validator_outer.equals
exports.contains = validator_outer.contains
exports.matches = validator_outer.matches
exports.isEmail = validator_outer.isEmail
exports.isURL = validator_outer.isURL
exports.isMACAddress = validator_outer.isMACAddress
exports.isIP = validator_outer.isIP
exports.isIPRange = validator_outer.isIPRange
exports.isFQDN = validator_outer.isFQDN
exports.isBoolean = validator_outer.isBoolean
exports.isIBAN = validator_outer.isIBAN
exports.isBIC = validator_outer.isBIC
exports.isAlpha = validator_outer.isAlpha
exports.isAlphaLocales = validator_outer.isAlphaLocales
exports.isAlphanumeric = validator_outer.isAlphanumeric
exports.isAlphanumericLocales = validator_outer.isAlphanumericLocales
exports.isNumeric = validator_outer.isNumeric
exports.isPassportNumber = validator_outer.isPassportNumber
exports.isPort = validator_outer.isPort
exports.isLowercase = validator_outer.isLowercase
exports.isUppercase = validator_outer.isUppercase
exports.isAscii = validator_outer.isAscii
exports.isFullWidth = validator_outer.isFullWidth
exports.isHalfWidth = validator_outer.isHalfWidth
exports.isVariableWidth = validator_outer.isVariableWidth
exports.isMultibyte = validator_outer.isMultibyte
exports.isSemVer = validator_outer.isSemVer
exports.isSurrogatePair = validator_outer.isSurrogatePair
exports.isInt = validator_outer.isInt
exports.isIMEI = validator_outer.isIMEI
exports.isFloat = validator_outer.isFloat
exports.isFloatLocales = validator_outer.isFloatLocales
exports.isDecimal = validator_outer.isDecimal
exports.isHexadecimal = validator_outer.isHexadecimal
exports.isOctal = validator_outer.isOctal
exports.isDivisibleBy = validator_outer.isDivisibleBy
exports.isHexColor = validator_outer.isHexColor
exports.isRgbColor = validator_outer.isRgbColor
exports.isHSL = validator_outer.isHSL
exports.isISRC = validator_outer.isISRC
exports.isMD5 = validator_outer.isMD5
exports.isHash = validator_outer.isHash
exports.isJWT = validator_outer.isJWT
exports.isJSON = validator_outer.isJSON
exports.isEmpty = validator_outer.isEmpty
exports.isLength = validator_outer.isLength
exports.isLocale = validator_outer.isLocale
exports.isByteLength = validator_outer.isByteLength
exports.isUUID = validator_outer.isUUID
exports.isMongoId = validator_outer.isMongoId
exports.isAfter = validator_outer.isAfter
exports.isBefore = validator_outer.isBefore
exports.isIn = validator_outer.isIn
exports.isCreditCard = validator_outer.isCreditCard
exports.isIdentityCard = validator_outer.isIdentityCard
exports.isEAN = validator_outer.isEAN
exports.isISIN = validator_outer.isISIN
exports.isISBN = validator_outer.isISBN
exports.isISSN = validator_outer.isISSN
exports.isMobilePhone = validator_outer.isMobilePhone
exports.isMobilePhoneLocales = validator_outer.isMobilePhoneLocales
exports.isPostalCode = validator_outer.isPostalCode
exports.isPostalCodeLocales = validator_outer.isPostalCodeLocales
exports.isEthereumAddress = validator_outer.isEthereumAddress
exports.isCurrency = validator_outer.isCurrency
exports.isBtcAddress = validator_outer.isBtcAddress
exports.isISO8601 = validator_outer.isISO8601
exports.isRFC3339 = validator_outer.isRFC3339
exports.isISO31661Alpha2 = validator_outer.isISO31661Alpha2
exports.isISO31661Alpha3 = validator_outer.isISO31661Alpha3
exports.isBase32 = validator_outer.isBase32
exports.isBase58 = validator_outer.isBase58
exports.isBase64 = validator_outer.isBase64
exports.isDataURI = validator_outer.isDataURI
exports.isMagnetURI = validator_outer.isMagnetURI
exports.isMimeType = validator_outer.isMimeType
exports.isLatLong = validator_outer.isLatLong
exports.ltrim = validator_outer.ltrim
exports.rtrim = validator_outer.rtrim
exports.trim = validator_outer.trim
exports.escape = validator_outer.escape
exports.unescape = validator_outer.unescape
exports.stripLow = validator_outer.stripLow
exports.whitelist = validator_outer.whitelist
exports.blacklist = validator_outer.blacklist
exports.isWhitelisted = validator_outer.isWhitelisted
exports.normalizeEmail = validator_outer.normalizeEmail
exports.isSlug = validator_outer.isSlug
exports.isStrongPassword = validator_outer.isStrongPassword
exports.isTaxID = validator_outer.isTaxID
exports.isDate = validator_outer.isDate
exports.isVAT = validator_outer.isVAT


exports.isPhone = isPhone
exports.isFunction = isFunction
exports.isIDCard = isIDCard
exports.isNullOrEmpty = isNullOrEmpty
exports.isEmail = isEmail

// var _default = validator;
// exports.default = _default;
// module.exports = exports.default;
// module.exports.default = exports.default;